<!-- 仓位管理 -->
<template>
  <div class="mod-config">
      <el-button-group style="margin-bottom: 10px;">
        <el-button icon="el-icon-plus" type="primary" v-if="permissions.eims_stock_add" @click="addOrUpdateHandle()">新增</el-button>
      </el-button-group>
      <div class="avue-crud">
        <el-table :data="dataList" height="calc(100vh - 215px)" border v-loading="dataListLoading" :max-height="tableHeight">
          <el-table-column prop="sort" width="80px" label="序号"></el-table-column>
          <el-table-column prop="code" width="80px" label="仓位编码"></el-table-column>
          <el-table-column prop="name" label="仓位名称"></el-table-column>
          <el-table-column prop="deptName" label="所属部门"></el-table-column>
          <el-table-column prop="status" label="状态">
            <template slot-scope="scope">
              <div v-if="scope.row.status == '1'">启用</div>
              <div v-if="scope.row.status == '0'">禁用</div>
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right">
            <template slot-scope="scope">
              <el-button v-if="permissions.eims_stock_edit" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
              <el-button v-if="permissions.eims_stock_del" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button>
              <el-button v-if="permissions.eims_stock_edit && scope.row.status == '1'" type="text" icon="el-icon-s-opportunity" @click="updateStockStatus(scope.row)">停用</el-button>
              <el-button v-if="permissions.eims_stock_edit && scope.row.status == '0'" type="text" icon="el-icon-s-opportunity" @click="updateStockStatus(scope.row)">启用</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="avue-crud__pagination">
        <el-pagination
					@size-change="sizeChangeHandle"
					@current-change="currentChangeHandle"
					:current-page="pageIndex"
					:page-sizes="[10, 20, 50, 100]"
					:page-size="pageSize"
					:total="totalPage"
					background
					layout="total, sizes, prev, pager, next">
        </el-pagination>
      </div>
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
  </div>
</template>

<script>
  import {fetchStockList, delStockObj, putStockObj} from '@/api/eims/stock'
  import {fetchTree} from '@/api/admin/dept'
  import TableForm from './form'
  import {mapGetters} from 'vuex'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 20,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false
      }
    },
    components: {
      TableForm
    },
    created () {
      this.getDataList()
    },
    computed: {
      ...mapGetters(['permissions']),
      tableHeight: function(){
        return window.innerHeight - 260
      }
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        fetchStockList(Object.assign({
          current: this.pageIndex,
          size: this.pageSize
        })).then(response => {
					if(response.data.code == '0'){
					  this.dataList = response.data.data.records
					  this.totalPage = response.data.data.total
					}else{
					  this.$message.error(response.data.msg)
					}
					this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        this.$confirm('是否确认删除ID为' + id, '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(function () {
          return delStockObj(id)
        }).then(response => {
					if(response.data.code == '0'){
					  this.$message.success('删除成功')
					  this.getDataList()
					}else{
					  this.$message.error(response.data.msg)
					}
        })
      },
      // 仓位停用-启用状态改变
      updateStockStatus (stockObj) {
        let text = (stockObj.status === '1') ? '停用' : '启用'
        // 把供应商状态改为想要的状态
        let status = (stockObj.status === '1') ? '0' : '1'
        this.$confirm('是否确认'+text+' '+stockObj.name,'提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          var paramsData = {id: stockObj.id,status: status}
          return putStockObj(paramsData)
        }).then(response => {
					if(response.data.code == '0'){
					  this.$message.success(text+'成功')
					  this.getDataList()
					}else{
					  this.$message.error(response.data.msg)
					}
        })
      }
    }
  }
</script>
